package controller

import (
	"log"
	"net/http"
	"os"
	"path/filepath"

	"github.com/gin-gonic/gin"
)

func ListFilesHandler(c *gin.Context) {
	log.Println("Handling list-files request")
	files, err := filepath.Glob(filepath.Join(uploadPath, "*"))
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": "读取文件时出错"})
		return
	}

	var fileList []struct {
		Name string `json:"name"`
		Size int64  `json:"size"`
	}

	for _, file := range files {
		fileInfo, err := os.Stat(file)
		if err != nil {
			log.Printf("获取文件信息时出错: %v", err)
			continue
		}
		fileList = append(fileList, struct {
			Name string `json:"name"`
			Size int64  `json:"size"`
		}{
			Name: filepath.Base(file),
			Size: fileInfo.Size(),
		})
	}

	c.JSON(http.StatusOK, fileList)
}
